<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class User extends Model
{
    //
    protected $table = 'users';

    protected $fillable = [
        'profile', 'nick_name', 'open_id'
    ];

    //获取购物车
    public static function getShoppingCart($user_id) {
        $list = json_decode(Redis::get('orderdinner:user'.$user_id.':shopping_cart'), true);
        $total_num = Redis::get('orderdinner:user'.$user_id.':shopping_cart_num');

        if(empty($list)) $list = [];
        if(empty($total_num)) $total_num = 0;

        $data = [
            'list' => $list,
            'total_num' => $total_num
        ];
        return $data;
    }

    public static function updateShoppingCart($user_id, $json_list, $total_num) {
        Redis::setex('orderdinner:user'.$user_id.':shopping_cart', 600, $json_list);
        Redis::setex('orderdinner:user'.$user_id.':shopping_cart_num', 600, $total_num);
    }

    public static function deleteShoppingCartAll($user_id) {
        Redis::del('orderdinner:user'.$user_id.':shopping_cart');
        Redis::del('orderdinner:user'.$user_id.':shopping_cart_num');
    }
}
